/*
 * InfoGamePanel.java
 *
 * Created on 8 mei 2008, 15:46
 */

package be.hogent.iii.project.client.gui.gameroom;

import be.hogent.iii.project.common.bericht.GeefSpelenBericht;
import be.hogent.iii.project.common.bericht.SpelWijzigingBericht;
import be.hogent.iii.project.common.game.objecten.Spel;
import be.hogent.iii.project.common.game.objecten.Speler;
import com.thoughtworks.xstream.XStream;
import java.util.List;
import javax.swing.DefaultListModel;

/**
 *
 * @author  Caribou
 */
public class InfoGamePanel extends javax.swing.JPanel {
    
    private Spel geselecteerdeSpel;
    private DefaultListModel listModel;
    
    /** Creates new form InfoGamePanel */
    public InfoGamePanel() {
        initComponents();
        listModel = new DefaultListModel();
        this.deelnemersLst.setModel(listModel);

        jPanel1.setVisible(false);
    }
    
    public void setSpel(Spel spel){
        geselecteerdeSpel = spel;
         jPanel1.setVisible(true);
         eigenaarLbl.setText("Dit spel is aangemaakt door " + spel.getEigenaar().getNick());
        jLabel2.setText("Maximum " + spel.getMaxSpelers() + " spelers");
        listModel.clear();
        //geselecteerdeSpel.addDeelnemer(GameRoomGuiFrame.getGameRoomGuiFrame().getGameRoomGui().getSpeler());
        List<Speler> deelnemers = geselecteerdeSpel.getDeelnemers();
        for(int i = 0; i < deelnemers.size(); i++){
            listModel.addElement(deelnemers.get(i).getNick());
        }
         }
 

    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        joinBtn = new javax.swing.JButton();
        leaveBtn = new javax.swing.JButton();
        startSpelBtn = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        deelnemersLst = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        eigenaarLbl = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        nieuwSpelBtn = new javax.swing.JButton();

        setBackground(new java.awt.Color(0, 0, 0));

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));

        joinBtn.setBackground(new java.awt.Color(102, 0, 0));
        joinBtn.setFont(new java.awt.Font("Tahoma", 1, 11));
        joinBtn.setForeground(new java.awt.Color(204, 153, 0));
        joinBtn.setText("Join");
        joinBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                joinBtnActionPerformed(evt);
            }
        });

        leaveBtn.setBackground(new java.awt.Color(102, 0, 0));
        leaveBtn.setFont(new java.awt.Font("Tahoma", 1, 11));
        leaveBtn.setForeground(new java.awt.Color(204, 153, 0));
        leaveBtn.setText("Leave");
        leaveBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                leaveBtnActionPerformed(evt);
            }
        });

        startSpelBtn.setBackground(new java.awt.Color(102, 0, 0));
        startSpelBtn.setFont(new java.awt.Font("Tahoma", 1, 11));
        startSpelBtn.setForeground(new java.awt.Color(204, 153, 0));
        startSpelBtn.setText("Start spel");
        startSpelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startSpelBtnActionPerformed(evt);
            }
        });

        deelnemersLst.setBackground(new java.awt.Color(102, 0, 0));
        deelnemersLst.setFont(new java.awt.Font("Tahoma", 1, 11));
        deelnemersLst.setForeground(new java.awt.Color(204, 153, 0));
        jScrollPane1.setViewportView(deelnemersLst);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Deze spelers zijn ingeschreven: ");

        eigenaarLbl.setFont(new java.awt.Font("Tahoma", 3, 11));
        eigenaarLbl.setForeground(new java.awt.Color(255, 255, 255));
        eigenaarLbl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        eigenaarLbl.setText("Spelnaam + info");

        jLabel2.setText("jLabel2");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(eigenaarLbl, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(7, 7, 7)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(startSpelBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(leaveBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
                            .addComponent(joinBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE))))
                .addGap(27, 27, 27))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(eigenaarLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(leaveBtn)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(startSpelBtn)
                                    .addGap(48, 48, 48))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jLabel2))))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(joinBtn)
                            .addGap(107, 107, 107)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(122, Short.MAX_VALUE))
        );

        nieuwSpelBtn.setBackground(new java.awt.Color(102, 0, 0));
        nieuwSpelBtn.setFont(new java.awt.Font("Tahoma", 1, 11));
        nieuwSpelBtn.setForeground(new java.awt.Color(204, 153, 0));
        nieuwSpelBtn.setText("Maak nieuw spel");
        nieuwSpelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nieuwSpelBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(302, Short.MAX_VALUE)
                .addComponent(nieuwSpelBtn)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(nieuwSpelBtn)
                .addContainerGap(14, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void startSpelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startSpelBtnActionPerformed
        GameRoomGuiFrame.getGameRoomGuiFrame().getGameRoomGui().startGame(geselecteerdeSpel);
    }//GEN-LAST:event_startSpelBtnActionPerformed

    private void nieuwSpelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nieuwSpelBtnActionPerformed
        NieuwSpelFrame.getNieuwSpelFrame().setVisible(true);
        NieuwSpelFrame.getNieuwSpelFrame().setConnectie(GameRoomGuiFrame.getGameRoomGuiFrame().getGameRoomGui().getconnectie());

    }//GEN-LAST:event_nieuwSpelBtnActionPerformed

    private void joinBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_joinBtnActionPerformed
        listModel.clear();
        geselecteerdeSpel.addDeelnemer(GameRoomGuiFrame.getGameRoomGuiFrame().getGameRoomGui().getSpeler());
        List<Speler> deelnemers = geselecteerdeSpel.getDeelnemers();
        for(int i = 0; i < deelnemers.size(); i++){
            listModel.addElement(deelnemers.get(i).getNick());
        }
        SpelWijzigingBericht wijziging = new SpelWijzigingBericht(geselecteerdeSpel);
        //GeefSpelenBericht spelInfo = new GeefSpelenBericht();
        XStream xStream = new XStream();
        GameRoomGuiFrame.getGameRoomGuiFrame().getConnectie().stuurBericht(xStream.toXML(wijziging));
        //GameRoomGuiFrame.getGameRoomGuiFrame().getConnectie().stuurBericht(xStream.toXML(spelInfo));      
    }//GEN-LAST:event_joinBtnActionPerformed

    private void leaveBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_leaveBtnActionPerformed
        listModel.removeElement(GameRoomGuiFrame.getGameRoomGuiFrame().getGameRoomGui().getSpeler().getNick());
        geselecteerdeSpel.verwijderDeelnemer(GameRoomGuiFrame.getGameRoomGuiFrame().getGameRoomGui().getSpeler());
        SpelWijzigingBericht wijziging = new SpelWijzigingBericht(geselecteerdeSpel);
        //GeefSpelenBericht spelInfo = new GeefSpelenBericht();
        XStream xStream = new XStream();
        GameRoomGuiFrame.getGameRoomGuiFrame().getConnectie().stuurBericht(xStream.toXML(wijziging));
        //GameRoomGuiFrame.getGameRoomGuiFrame().getConnectie().stuurBericht(xStream.toXML(spelInfo));
    }//GEN-LAST:event_leaveBtnActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList deelnemersLst;
    private javax.swing.JLabel eigenaarLbl;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton joinBtn;
    private javax.swing.JButton leaveBtn;
    private javax.swing.JButton nieuwSpelBtn;
    private javax.swing.JButton startSpelBtn;
    // End of variables declaration//GEN-END:variables
    
}
